package net.web.inst.struts2;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.esj.basic.exception.MelonException;
import net.esj.basic.utils.MathRandomUtils;
import net.esj.basic.utils.StringUtils;
import net.esj.sms.SMSUtils;
import net.melon.agency.inst.ssh.service.AgencyService;
import net.melon.agency.pojo.Agency;
import net.melon.sso.pojo.Member;
import net.melon.sso.service.SSOService;
import net.web.dto.BaseParam;
import net.web.dto.MemberDto;

public class LoginAction extends WebAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3112219900374609548L;
	
	@Resource
	private SSOService ssoService;

	private String un;
	
	private String pwd;
	
	private String code;
	
	private Member member;
	
	private String agencyId; 
	
	@Resource
	private AgencyService agencyService;
	
	public void login(){
		try{
			if(getSessionMember()!=null){
				map.put("suc", suc);
				map.put("member", getMemberDto(getSessionMember()));
			}else{
				if(StringUtils.hasText(un) && StringUtils.hasLength(pwd)){
					member = ssoService.normalLogin(un, pwd);
					map.put("suc", suc);
					map.put("member", getMemberDto(member));
				}else{
					map.put("suc", fail);
					map.put("msg", "登录失败");
				}
			}
			
		}catch(MelonException e){
			map.put("suc", fail);
			map.put("msg", "登录失败");
		}catch(Exception e){
			map.put("suc", fail);
			map.put("msg", "登录失败");
		}
		writeJson(map);
	}
	
	public void reg(){
		if(StringUtils.hasText(code) && code.equals(codeSession.get(un))){
			if(StringUtils.hasText(un) && StringUtils.hasText(pwd)){
				try {
					member = new Member();
					member.setMobile(un);
					member.setPassword(pwd);
					member.setState(1);
					member.setAgencyId(agencyId);
					member = ssoService.reg(member);
					map.put("suc", suc);
					map.put("member", getMemberDto(member));
				} catch (MelonException e) {
					map.put("suc", fail);
					map.put("msg", "该号码已经注册");
				}catch (Exception e) {
					e.printStackTrace();
					map.put("suc", fail);
					map.put("msg", "注册失败");
				}
			}else{
				map.put("suc", fail);
				map.put("msg", "号码或密码或代理商不能为空");
			}
		}else {
			map.put("suc", fail);
			map.put("msg", "验证码错误");
		}
		writeJson(map);
	}
	
	public void send(){
		//发送短信
		try {
			if(StringUtils.hasText(un)){
				String code = MathRandomUtils.getRandom(6, 10);
				codeSession.put(un, code);
				if(SMSUtils.AlibabaSMSSend(un, code)){
					map.put("suc", suc);
					map.put("msg", "发送成功");
				}else{
					map.put("suc", fail);
					map.put("msg", "发送失败");
				}
			}else{
				map.put("suc", fail);
				map.put("msg", "发送失败");
			}
		} catch (Exception e) {
			map.put("suc", fail);
			map.put("msg", "发送失败");
		}
		writeJson(map);
	}
	
	public void editPass(){
		try {
			if(StringUtils.hasText(code) && StringUtils.hasText(un) && code.equals(codeSession.get(un))){
				ssoService.editPass(un,pwd);
				map.put("suc", suc);
			}else{
				map.put("suc", fail);
				map.put("msg", "验证码错误");
			}
		} catch (MelonException e) {
			map.put("suc", fail);
			map.put("msg", "修改失败");
		} catch (Exception e) {
			map.put("suc", fail);
			map.put("msg", "修改失败");
		}
		writeJson(map);
	}
	
	public void logout(){
		try {
			ssoService.logout();
			map.put("suc", suc);
		} catch (Exception e) {
			map.put("suc", fail);
		}
		writeJson(map);
	}
	
	public void getAgencyList(){
		try {
			List<Agency> agencyList = agencyService.listAgencyByCity(getCitySession());
			List<BaseParam> list = new ArrayList<BaseParam>();
			for(Agency agency : agencyList){
				list.add(new BaseParam(agency.getId(), agency.getName()));
			}
			map.put("suc", suc);
			map.put("list", list);
			writeJson(map);
		} catch (Exception e) {
			map.put("suc", fail);
		}
		
	}
	
	public void checkUnique(){
		if(StringUtils.hasText(un)&&ssoService.checkMemberUnique(un)){
			map.put("suc", suc);
		}else{
			map.put("suc", fail);
		}
		writeJson(map);
	}
	
	
	protected MemberDto getMemberDto(Member member) {
		MemberDto dto = new MemberDto(member.getId(),member.getMobile());
		return dto;
	}
	
	public String getUn() {
		return un;
	}

	public void setUn(String un) {
		this.un = un;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getAgencyId() {
		return agencyId;
	}

	public void setAgencyId(String agencyId) {
		this.agencyId = agencyId;
	}
	
}
